#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/12/18 22:20
# @File           : views.py
# @IDE            : PyCharm
# @desc           : 路由，视图文件

from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import APIRouter, Depends
from . import models, schemas, crud, params
from core.dependencies import IdList
from apps.vadmin.auth.utils.current import AllUserAuth
from utils.response import SuccessResponse
from apps.vadmin.auth.utils.validation.auth import Auth
from core.database import db_getter


app = APIRouter()


###########################################################
#    磅房管理
###########################################################
@app.get("/weigh", summary="获取磅房管理列表", tags=["磅房管理"])
async def get_inventory_list(p: params.InventoryParams = Depends(), auth: Auth = Depends(AllUserAuth())):
    datas, count = await crud.InventoryDal(auth.db).get_datas(**p.dict(), v_return_count=True)
    return SuccessResponse(datas, count=count)


@app.post("/weigh", summary="创建磅房管理", tags=["磅房管理"])
async def create_inventory(data: schemas.Inventory, auth: Auth = Depends(AllUserAuth())):
    return SuccessResponse(await crud.InventoryDal(auth.db).create_data(data=data))


@app.delete("/weigh", summary="删除磅房管理", description="硬删除", tags=["磅房管理"])
async def delete_inventory_list(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
    await crud.InventoryDal(auth.db).delete_datas(ids=ids.ids, v_soft=False)
    return SuccessResponse("删除成功")


@app.put("/weigh/{data_id}", summary="更新磅房管理", tags=["磅房管理"])
async def put_inventory(data_id: int, data: schemas.Inventory, auth: Auth = Depends(AllUserAuth())):
    return SuccessResponse(await crud.InventoryDal(auth.db).put_data(data_id, data))


@app.get("/weigh/{data_id}", summary="获取磅房管理信息", tags=["磅房管理"])
async def get_inventory(data_id: int, db: AsyncSession = Depends(db_getter)):
    schema = schemas.InventorySimpleOut
    return SuccessResponse(await crud.InventoryDal(db).get_data(data_id, v_schema=schema))

